package androidserver;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.ActionMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author felix
 */
public class AndroidServerSettingBox extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	
	private JButton addButton;
    private JButton deleteButton;
    private JLabel jLabel2;
    private JScrollPane jScrollPane1;
    private JTable table;
    private DefaultTableModel model;
    
    public AndroidServerSettingBox(java.awt.Frame parent) {
    	super(parent);
        initComponents();
    }
    
    public Map<String, String> getBaseStation(){
    	Map<String, String> map = new HashMap<String, String>();
    	Vector<Vector> v = model.getDataVector();
    	for(Vector vv : v){
    		map.put( vv.get(0).toString(), vv.get(1).toString() );
    	}
    	return map;
    }
    
    @Action
    public void addRow(){
    	model.insertRow(table.getRowCount(), new Object []{});
    }
    
    @Action
    public void deleteRow(){
    	int i = table.getSelectedRow();
    	if( i>=0 )
    		model.removeRow( i );
    	else 
    		JOptionPane.showConfirmDialog(this, "Please select a row!", "Note", JOptionPane.CLOSED_OPTION);
    }
    
    private void initComponents() {
    	ActionMap actionMap = Application.getInstance(androidserver.AndroidServerApp.class).getContext().getActionMap(AndroidServerSettingBox.class, this);
    	ResourceMap resourceMap = Application.getInstance(androidserver.AndroidServerApp.class).getContext().getResourceMap(AndroidServerSettingBox.class);
    	
    	model = new DefaultTableModel(
    				new Object [][] {},
    				new String [] {
                		resourceMap.getString("jTable1.columnModel.title0"), resourceMap.getString("jTable1.columnModel.title1")
    				}
            	);
    	jLabel2 = new javax.swing.JLabel();
    	jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        
    	jScrollPane1 = new javax.swing.JScrollPane();
    	jScrollPane1.setName("jScrollPane1"); // NOI18N
    	
        addButton = new javax.swing.JButton();
        addButton.setAction( actionMap.get("addRow") );
        addButton.setName(""); // NOI18N
        
        deleteButton = new javax.swing.JButton();
        deleteButton.setAction( actionMap.get("deleteRow") );
        deleteButton.setName("jButton2"); // NOI18N

        
        table = new javax.swing.JTable();
        table.setName("jTable1"); // NOI18N
        table.setModel(model);
        
        jScrollPane1.setViewportView(table);

        setName("Form"); // NOI18N
        setModal(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(addButton)
                            .addComponent(deleteButton)))
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(addButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)))
                .addContainerGap(110, Short.MAX_VALUE))
        );
    }
}
